#!/usr/bin/env bash
###
# 设置Ansible部署环境
###
#set -x
set -euo pipefail

cd $(dirname ${BASH_SOURCE[0]})
SHELL_FOLDER=$(pwd)
DEPLOY_ROOT=$(cd ../ && pwd)
source $DEPLOY_ROOT/init/base.sh

mkdir -pv $DEPLOY_ROOT/packages

INFO "Modify ansible inventory.yaml"
yq -i -e '.all.vars.download_dir = "'"$DEPLOY_ROOT/packages"'"' $SHELL_FOLDER/inventory.yaml
yq -i -e '.all.vars.download_url = "'"$DOWNLOAD_URL"'"' $SHELL_FOLDER/inventory.yaml
INFO "Show ansible vars"
yq -e '.all.vars' $SHELL_FOLDER/inventory.yaml

INFO "Check ansible inventory.yaml"
ansible -i $SHELL_FOLDER/inventory.yaml all --list
INFO "ansible gather_facts of all nodes"
ansible -i $SHELL_FOLDER/inventory.yaml all -m setup &> /dev/null
INFO "ansible gather_facts of localhost"
ansible localhost -m setup &> /dev/null

INFO "Generate $HOME/.ansible.cfg"
ansible -i $SHELL_FOLDER/inventory.yaml localhost -c local -m template \
-a "src=$SHELL_FOLDER/ansible.cfg.tpl dest=$HOME/.ansible.cfg" \
-e "current_pwd=$SHELL_FOLDER ansible_user=$USER"

ls -alh $HOME/.ansible.cfg
INFO "Generate $HOME/.ansible.cfg DOEN"